<?php
    
    //datafunctions.php
    
     require("dbconnector.php");
     require("config.php");
     
     $action = $_POST['action'];                //获取行为命令
     $format = $_POST['format'];                //获取返回格式
     switch ($action) {                         //判断行为
        case "gettalks":                        //获取若干个talks
            $tag = $_POST['tag'];               //获取tag
            $tcound = $_POST['tcound'];         //获取请求的talk数量
            $tscound = $_POST['tscound'];
            gettalks($tag,$tscound,$tcound,$format);
            break;
        case "gettalk":                         //获取指定的talk
            $tid = $_POST['tid'];               //获取talk的id
            gettalk($tid,$format);
            break;
        case "getuserinfo":                     //获取指定用户信息
            $email = $_POST['email'];           //获取请求的用户的Email
            getuserinfo($email);
            break;
        case "search":                          //搜索
            $seastr = $_POST['s'];              //获取搜索关键词
            search($seastr,$format);
            break;
        default:
            returnerror();                      //返回错误
    }
    function gettalks($tag,$scound,$cound,$format) {//返回若干talks函数
        if ($tag == undefined || $tag == "") {
            $quertstr = "SELECT title,author,commentscound,date FROM talks";
        }else {
            $quertstr = "SELECT title,author,commentscound,date FROM talks WHERE tag = '".$tag."'";
        }
        $queryresult = mysql_query($querystr);
        $queryarray = mysql_fetch_array($queryresult);
        if ($format != "xml"){
            echo "[";
            for ($i = $scound;$i < $cound + $scound;$i++){
                echo "{";
                echo '"title":'.'"'.$queryarray['title'][$i].'",';
                echo '"author":'.'"'.$queryarray['author'][$i].'",';
                echo '"date":'.'"'.$queryarray['date'][$i].'",';
                echo '"commentscound":'.'"'.$queryarray['commentscound'][$i].'"';
                echo "}";
                if ($i < $cound + $scound) {
                    echo ",";
                }
            }
            echo "]";
        } else {
            echo '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel> <title>';
            echo $GLOBALS["sitename"]."</title>";
            for ($i = $scound;$i < $cound+$scound;$i++){
                echo "<item>";
                echo "<title>".$queryarray['title'][$i]."</title>";
                echo "<author>".$queryarray['author'][$i]."</author>";
                echo "<date>".$queryarray['date'][$i]."</date>";
                echo "<commentscound>".$queryarray['commentscound'][$i]."</commentscound>";
                echo "</item>";
            }
            echo "</channel></rss>";
        }
    }
    function gettalk($tid,$format) {
        $quertstr = "SELECT title,author,content,comments,date FROM talks WHERE id = '".$tid."'";
        $queryresult = mysql_query($querystr);
        $queryarray = mysql_fetch_array($queryresult);
        $xmlcomments = json_decode($queryarray['comments']);
        if ($format != "xml"){
            echo "[";
            for ($i = $scound;$i < $cound + $scound;$i++){
                echo "{";
                echo '"title":'.'"'.$queryarray['title'][$i].'",';
                echo '"author":'.'"'.$queryarray['author'][$i].'",';
                echo '"content":'.'"'.$queryarray['content'][$i].'",';
                echo '"date":'.'"'.$queryarray['date'][$i].'",';
                echo '"comments":'.'"'.$queryarray['comments'][$i].'"';
                echo "}";
            }
            echo "]";
        } else {
            echo '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel> <title>';
            echo $GLOBALS["sitename"]."</title>";
            for ($i = $scound;$i < $cound + $scound;$i++){
                echo "<item>";
                echo "<title>".$queryarray['title'][$i]."</title>";
                echo "<author>".$queryarray['author'][$i]."</author>";
                echo "<content>".$queryarray['content'][$i]."</content>";
                echo "<date>".$queryarray['date'][$i]."</date>";
                echo "<comments>";
                for ($j = 0;$j < $xmlcomments.length;$j++){
                    echo "<comment>";
                    echo "<comment:author>".$xmlcomments[$i]['author']."</comment:author>";
                    echo "<comment:content>".$xmlcomments[$i]['content']."</comment:content>";
                    echo "<comment:date>".$xmlcomments[$i]['date']."</comment:date>";
                    echo "</comment>";
                }
                echo "</comments>";
                echo "</item>";
            }
            echo "</channel></rss>";
        }
    }
    function getuserinfo($email,$format) {
        $quertstr = "SELECT name,website,description FROM users WHERE email = '".$email."'";
        $queryresult = mysql_query($querystr);
        $queryarray = mysql_fetch_array($queryresult);
        if ($format != "xml"){
            echo "[{";
            echo '"name":'.'"'.$queryarray['name'].'",';
            echo '"website":'.'"'.$queryarray['website'].'",';
            echo '"description":'.'"'.$queryarray['description'].'"';
            echo "}]";
        } else {
            echo '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel> <title>';
            echo $GLOBALS["sitename"]."</title>";
            echo "<item><name>".$queryarray['name']."</name>";
            echo "<website>".$queryarray['website']."</website>";
            echo "<description>".$queryarray['description']."</description>";
            echo "</item></channel></rss>";
        }
    }
    function search($s,$format) {
        //等待编辑。。
    }
    function returnerror(){
        echo "提交行为错误！";
    }
?>